﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.TournamentWonLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class TournamentWonLogEntry : LogEntry, IEncyclopediaLog, IChatNotification
  {
    [SaveableField(934)]
    private MBReadOnlyDictionary<Hero, short> _participants;

    internal static void AutoGeneratedStaticCollectObjectsTournamentWonLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this._participants);
      collectedObjects.Add((object) this.Winner);
      collectedObjects.Add((object) this.Town);
    }

    internal static object AutoGeneratedGetMemberValueWinner(object o)
    {
      return (object) ((TournamentWonLogEntry) o).Winner;
    }

    internal static object AutoGeneratedGetMemberValueTown(object o)
    {
      return (object) ((TournamentWonLogEntry) o).Town;
    }

    internal static object AutoGeneratedGetMemberValue_participants(object o)
    {
      return (object) ((TournamentWonLogEntry) o)._participants;
    }

    [SaveableProperty(931)]
    public Hero Winner { get; private set; }

    [SaveableProperty(932)]
    public Town Town { get; private set; }

    public bool IsVisibleNotification => true;

    public override ChatNotificationType NotificationType
    {
      get => this.CivilianNotification((IFaction) this.Winner.Clan);
    }

    public TournamentWonLogEntry(
      Hero winner,
      Town town,
      MBReadOnlyList<CharacterObject> gameParticipants)
    {
      this.Winner = winner;
      this.Town = town;
      Dictionary<Hero, short> dictionary = new Dictionary<Hero, short>();
      foreach (CharacterObject gameParticipant in (List<CharacterObject>) gameParticipants)
      {
        if (gameParticipant.HeroObject != null && !dictionary.ContainsKey(gameParticipant.HeroObject))
          dictionary.Add(gameParticipant.HeroObject, (short) 1);
      }
      this._participants = dictionary.GetReadOnlyDictionary<Hero, short>();
    }

    public override string ToString() => "Tournament won";

    public TextObject GetEncyclopediaText() => this.GetNotificationText();

    public TextObject GetNotificationText()
    {
      TextObject parent = new TextObject("{=4ADQ7YZj}{PERSON.LINK} won the tournament at {TOWN}.");
      StringHelpers.SetCharacterProperties("PERSON", this.Winner.CharacterObject, parent);
      parent.SetTextVariable("TOWN", this.Town.Name);
      return parent;
    }

    public bool IsVisibleInEncyclopediaPageOf<T>(T obj) where T : MBObjectBase
    {
      return (object) obj == this.Winner || (object) obj == this.Winner.Clan || (object) obj == this.Town.Settlement;
    }

    public override void GetConversationScoreAndComment(
      Hero talkTroop,
      bool findString,
      out string comment,
      out ImportanceEnum score)
    {
      short num1;
      if (!this._participants.TryGetValue(talkTroop, out num1))
        num1 = (short) -2;
      short num2;
      if (!this._participants.TryGetValue(Hero.MainHero, out num2))
        num2 = (short) -2;
      score = ImportanceEnum.Zero;
      comment = "";
      if (num1 <= (short) 0 || num2 <= (short) 0)
        return;
      score = ImportanceEnum.SomewhatImportant;
      if (!findString)
        return;
      MBTextManager.SetTextVariable("TOURNAMENT_TOWN", this.Town.Name, false);
      MBTextManager.SetTextVariable("TOURNAMENT_WINNER", this.Winner.Name, false);
      if (this.Winner == Hero.MainHero)
      {
        comment = "str_comment_you_won_tournament_elsewhere";
        if (this.Town.Settlement != Settlement.CurrentSettlement)
          return;
        comment = "str_comment_you_won_tournament_here";
      }
      else if (this.Winner == talkTroop)
      {
        comment = "str_comment_i_won_tournament_elsewhere";
        if (this.Town.Settlement != Settlement.CurrentSettlement)
          return;
        comment = "str_comment_i_won_tournament_here";
      }
      else
      {
        comment = "str_comment_someone_else_won_tournament_elsewhere";
        if (this.Town.Settlement != Settlement.CurrentSettlement)
          return;
        comment = "str_comment_someone_else_won_tournament_here";
      }
    }
  }
}
